package org.tpdata.api.cdss.AssistantDiagnosis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import common.Data;
import common.RunCaseJson;
import common.SetUpTearDown1;
import exceptions.TestNGRetry;
import io.qameta.allure.Story;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import static common.TestStep.requestAndRespondBody;

public class searchjson extends SetUpTearDown1 {

    Logger log = Logger.getLogger(searchjson.class);
    Data data = envSetUp();

    public  String createAnswer(){
        JSONObject json = new JSONObject();

//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("url","http://gyfyy.com/fhir/Questionnaire/zdyc-1594260387734" );
        log.info(json.toString());

        Response response = RunCaseJson.runCase(json, "get", "api-project/answer/createAnswer.json");

        String jsonStr = response.asString();
        JSONObject obj = JSON.parseObject(jsonStr);
        String str = obj.getJSONObject("data").getJSONObject("http://gyfyy.com/fhir/Questionnaire/zdyc-1594260387734").getString("answerId");
       return str;
    }

    String answerId= createAnswer();
    public  void  save(){
        JSONObject json = new JSONObject();

//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("org_ids", "true");
        json.put("qtId","478544593856630784");
        json.put("answerId",answerId);
        json.put("type","2");
        json.put("versionId","1");
        json.put("classname","diagnosticPredictionSave");
        json.put("context","[{\"linkId\":\"b0bf7f29-3a42-43c4-a518-9b561f488340\",\"sectionItemRId\":\"478548965374169088\",\"showType\":\"context-query\",\"value\":[{\"content\":\"咳嗽\"}]}]");
        Response response = RunCaseJson.runCase(json, "post", "/api-project/answer/save.json");
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
    }

    public String search(String uri){
        JSONObject json = new JSONObject();
//        Data data = envSetUp();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("orgUserId", data.getUserId());
        json.put("org_ids", "true");
        json.put("qtId","478544593856630784" );
        json.put("answerId", answerId);
        log.info(json.toString());

        Response response = RunCaseJson.runCase(json, "get", uri);
        String jsonStr = response.asString();
        String url = RestAssured.baseURI + ":" + RestAssured.port + "/" + uri;
        requestAndRespondBody(url,json,jsonStr);
        log.info("响应报文：" + jsonStr);

        return jsonStr;
    }


    @Story("辅助诊疗-诊断预测结果")
    @Parameters({"uri"})
    @Test(retryAnalyzer = TestNGRetry.class,description = "诊断预测结果要有数据" )
    public void runCase1(String uri) {
        save();
        String jsonStr=search(uri);
        JSONObject obj = JSON.parseObject(jsonStr);
        String string = obj.getJSONObject("responseData").getString("total");
        Assert.assertNotEquals(string,"0"); //判断total总数不等于0
    }


    @Parameters({"uri"})
    @Test(retryAnalyzer = TestNGRetry.class,description = "判断知识库存在数据" )
    public void runCase2(String uri) {
        String jsonStr=search(uri);
        JSONObject obj = JSON.parseObject(jsonStr);

        JSONArray susperctedRsList=obj.getJSONObject("responseData").getJSONArray("suspectedRsList");
        if (susperctedRsList.size()>0){
            JSONObject jsonObject=susperctedRsList.getJSONObject(0);
            String diseaseName=jsonObject.getString("diseaseName");
            String icdCode=jsonObject.getString("icdCode");

            //知识库的用例
            JSONObject json1 = new JSONObject();
            json1.put("access_token", data.getAccessToken());
            json1.put("authToken", data.getAccessToken());
            json1.put("uniqu_no", data.getUniquNo());
            json1.put("operatorId", data.getUserId());
            json1.put("authUserId", data.getUserId());
            json1.put("authName", data.getUserName());
            json1.put("org_ids", true);
            json1.put("name",diseaseName);
            log.info(json1.toString());
            Response response1 = RunCaseJson.runCase(json1, "get", "/api-knowledge/knowledge/diseases/data/get");
            String jsonStr1 = response1.asString();
            log.info("响应报文：" + jsonStr1);
            JSONObject obj1 = JSON.parseObject(jsonStr1);
            JSONArray contarr=obj1.getJSONArray("data");
            Assert.assertNotEquals(contarr.size(),"0");
        }
    }


    @Parameters({"uri"})
    @Test(retryAnalyzer = TestNGRetry.class,description = "判断相似病例存在数据" )
    public void runCase3(String uri) {
        String jsonStr=search(uri);
        JSONObject obj = JSON.parseObject(jsonStr);

        JSONArray susperctedRsList=obj.getJSONObject("responseData").getJSONArray("suspectedRsList");
        if (susperctedRsList.size()>0){
            JSONObject jsonObject=susperctedRsList.getJSONObject(0);
            String diseaseName=jsonObject.getString("diseaseName");
            String icdCode=jsonObject.getString("icdCode");
            JSONObject json2 = new JSONObject();
            json2.put("access_token", data.getAccessToken());
            json2.put("authToken", data.getAccessToken());
            json2.put("uniqu_no", data.getUniquNo());
            json2.put("operatorId", data.getUserId());
            json2.put("authUserId", data.getUserId());
            json2.put("authName", data.getUserName());
            json2.put("org_ids", true);
            json2.put("icdCode",icdCode);
            json2.put("page","1");
            json2.put("size","10");
            log.info(json2.toString());
            Response response2 = RunCaseJson.runCase(json2, "get", "/api-project/diagPredict/v2/getAllPatientByICD");
            String jsonStr2 = response2.asString();
            log.info("响应报文：" + jsonStr2);
            JSONObject obj2 = JSON.parseObject(jsonStr2);

            JSONArray contarr2=obj2.getJSONObject("responseData").getJSONArray("content");
            Assert.assertNotEquals(contarr2.size(),"0");
        }

    }



/*    public void runCase(String uri) {
        save();
        JSONObject json = new JSONObject();
//        Data data = envSetUp();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("orgUserId", data.getUserId());
        json.put("org_ids", "true");
        json.put("qtId","478544593856630784" );
        json.put("answerId", answerId);
        log.info(json.toString());

        Response response = RunCaseJson.runCase(json, "get", uri);
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);

        JSONObject obj = JSON.parseObject(jsonStr);
        String url = RestAssured.baseURI + ":" + RestAssured.port + "/" + uri;
        requestAndRespondBody(url,json,jsonStr);
        String string = obj.getJSONObject("responseData").getString("total");
        Assert.assertNotEquals(string,"0"); //判断total总数不等于0

        JSONArray susperctedRsList=obj.getJSONObject("responseData").getJSONArray("suspectedRsList");
            if (susperctedRsList.size()>0){
                JSONObject jsonObject=(JSONObject)susperctedRsList.get(0);
                String diseaseName=jsonObject.getString("diseaseName");
                String icdCode=jsonObject.getString("icdCode");

                //知识库的用例
                JSONObject json1 = new JSONObject();
                json1.put("access_token", data.getAccessToken());
                json1.put("authToken", data.getAccessToken());
                json1.put("uniqu_no", data.getUniquNo());
                json1.put("operatorId", data.getUserId());
                json1.put("authUserId", data.getUserId());
                json1.put("authName", data.getUserName());
                json1.put("org_ids", true);
                json1.put("name",diseaseName);
                log.info(json1.toString());
                Response response1 = RunCaseJson.runCase(json1, "get", "/api-knowledge/knowledge/diseases/data/get");
                String jsonStr1 = response1.asString();
                log.info("响应报文：" + jsonStr1);
                JSONObject obj1 = JSON.parseObject(jsonStr1);
                JSONArray contarr=obj1.getJSONArray("data");
                Assert.assertNotEquals(contarr.size(),"0");

            //相似病例的用例
            JSONObject json2 = new JSONObject();
            json2.put("access_token", data.getAccessToken());
            json2.put("authToken", data.getAccessToken());
            json2.put("uniqu_no", data.getUniquNo());
            json2.put("operatorId", data.getUserId());
            json2.put("authUserId", data.getUserId());
            json2.put("authName", data.getUserName());
            json2.put("org_ids", true);
            json2.put("icdCode",icdCode);
            json2.put("page","1");
            json2.put("size","10");
            log.info(json2.toString());
            Response response2 = RunCaseJson.runCase(json2, "get", "/api-project/diagPredict/v2/getAllPatientByICD");
            String jsonStr2 = response2.asString();
            log.info("响应报文：" + jsonStr2);
            JSONObject obj2 = JSON.parseObject(jsonStr2);

            JSONArray contarr2=obj2.getJSONObject("responseData").getJSONArray("content");
            Assert.assertNotEquals(contarr2.size(),"0");


        }

    }*/




}
